/*
   type: Point,Circle,Square,LineString,Box,Polygon
   Square正方形(Circle的一种) Box矩形(LineString的一种)
*/
function createDraw({ type, source }) {
  let geometryFunction = null;
  let maxPoints = null;
  if (type == "Square") {
    type = "Circle";
    geometryFunction = ol.interaction.Draw.createRegularPolygon(4);
  }
  if (type == "Box") {
    type = "LineString";
    maxPoints = 2;
    geometryFunction = function (coordinates, geometry) {
      // 在画的过程中实时自动获取coordinates和geometry
      if (!geometry) {
        //如果没有geometry，就创建一个geometry多边形
        geometry = new ol.geom.Polygon(null);
      }
      // coordinates会自动获取画线的起点和终点坐标，是一个二维数组
      var start = coordinates[0];
      var end = coordinates[1];
      geometry.setCoordinates([
        [start, [start[0], end[1]], end, [end[0], start[1]], start],
      ]);
      return geometry;
    };
  }
  let draw = new ol.interaction.Draw({
    type,
    source,
    geometryFunction,
    maxPoints,
  });

  return draw;
}
